package com.durian.ui.adapter.divider;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 说明：GridManager间距
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2017-10-12
 * <p/>
 * 版本：version 1.0
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int     spanCount    = 0;//每行个数
    private int     totalCount   = 0;//总数量
    private int     spacing      = 1;//每行间隙，单位px
    private int     halfSpacing  = 1;
    private boolean includeEdge  = true;//是否包含边线
    private int     spacingColor = Color.TRANSPARENT;
    private Paint   mPaint       = new Paint(Paint.ANTI_ALIAS_FLAG);

    private GridSpacingItemDecoration() {
    }

    public static GridSpacingItemDecoration create(int spacing) {
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration();
        decoration.spacing = spacing;
        decoration.halfSpacing = spacing / 2;
        return decoration;
    }

    /**
     * 是否包含边线
     *
     * @param includeEdge
     * @return
     */
    public GridSpacingItemDecoration isIncludeEdge(boolean includeEdge) {
        this.includeEdge = includeEdge;
        return this;
    }

    /**
     * 设置颜色
     *
     * @param color
     * @return
     */
    public GridSpacingItemDecoration setColor(int color) {
        this.spacingColor = color;
        return this;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (isGridLayoutManager(parent)) {
            int count = parent.getChildCount();
            for (int i = 0; i < count; i++) {
                View view = parent.getChildAt(i);
                int index = parent.getChildAdapterPosition(view);
                int left = view.getLeft();
                int right = view.getRight();
                int top = view.getTop();
                int bottom = view.getBottom();

                Rect decorationRect;
                mPaint.setColor(spacingColor);
                if (includeEdge) {
                    if (spanCount == 1) {
                        decorationRect = new Rect(left - spacing, top - spacing, right + spacing, bottom);
                    } else {
                        if (isFirstColumn(index)) {
                            decorationRect = new Rect(left - spacing, top - spacing, right + halfSpacing, bottom);
                        } else if (isLastColumn(index)) {
                            decorationRect = new Rect(left - halfSpacing, top - spacing, right + spacing, bottom);
                        } else {
                            if (isLastPosition(index)) {
                                decorationRect = new Rect(left - halfSpacing, top - spacing, right + spacing, bottom);
                            } else {
                                decorationRect = new Rect(left - halfSpacing, top - spacing, right + halfSpacing, bottom);
                            }
                        }
                    }
                    c.drawRect(decorationRect, mPaint);
                    if (isLastRow(index)) {
                        Rect bottomRect = new Rect(left - spacing, top, right + spacing, bottom + spacing);
                        c.drawRect(bottomRect, mPaint);
                    }
                } else {
                    if (spanCount == 1) {
                        decorationRect = new Rect(left - spacing, top, right + spacing, bottom);
                    } else {
                        if (isFirstColumn(index)) {
                            decorationRect = new Rect(left - spacing, top - spacing, right + halfSpacing, bottom);
                        } else if (isLastColumn(index)) {
                            decorationRect = new Rect(left - halfSpacing, top - spacing, right + spacing, bottom);
                        } else {
                            if (isLastPosition(index)) {
                                decorationRect = new Rect(left - halfSpacing, top - spacing, right + spacing, bottom);
                            } else {
                                decorationRect = new Rect(left - halfSpacing, top - spacing, right + halfSpacing, bottom);
                            }
                        }
                    }
                    if (!isLastRow(index)) {
                        Rect bottomRect = new Rect(left - spacing, top, right + spacing, bottom + spacing);
                        c.drawRect(bottomRect, mPaint);
                    }
                    c.drawRect(decorationRect, mPaint);
                }
            }
        }
    }


    private boolean isGridLayoutManager(RecyclerView parent) {
        return parent.getLayoutManager() instanceof GridLayoutManager;
    }

    /**
     * 初始化数据
     *
     * @param parent
     */
    private void init(RecyclerView parent) {
        if (spanCount == 0) {
            if (isGridLayoutManager(parent)) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
                spanCount = gridLayoutManager.getSpanCount();
            }
        }
        if (spanCount == 0) {
            spanCount = 1;
        }

        totalCount = parent.getAdapter().getItemCount();
    }

    /**
     * 是否是最后一行
     *
     * @param index
     * @return
     */
    private boolean isLastRow(int index) {
        int row = totalCount % spanCount == 0 ? totalCount / spanCount : totalCount / spanCount + 1;
        return index + spanCount >= row * spanCount;
    }

    /**
     * 是否是最后一个
     *
     * @param index
     * @return
     */
    private boolean isLastPosition(int index) {
        return index + 1 == totalCount;
    }

    /**
     * 是否是最后一列
     *
     * @param index
     * @return
     */
    private boolean isLastColumn(int index) {
        return (index + 1) % spanCount == 0;
    }

    /**
     * 是否是第一列
     *
     * @param index
     * @return
     */
    private boolean isFirstColumn(int index) {
        return index % spanCount == 0;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (isGridLayoutManager(parent)) {
            init(parent);
            int index = parent.getChildAdapterPosition(view);
            if (includeEdge) {
                outRect.top = spacing;
                if (spanCount == 1) {
                    outRect.left = spacing;
                    outRect.right = spacing;

                } else {
                    if (isFirstColumn(index)) {
                        outRect.left = spacing;
                        outRect.right = halfSpacing;
                    } else if (isLastColumn(index)) {
                        outRect.left = halfSpacing;
                        outRect.right = spacing;
                    } else {
                        outRect.left = halfSpacing;
                        if (isLastPosition(index)) {
                            outRect.right = spacing;
                        } else {
                            outRect.right = halfSpacing;
                        }
                    }
                }
                if (isLastRow(index)) {
                    outRect.bottom = spacing;
                }
            } else {
                if (spanCount == 1) {
                    if (!isLastRow(index)) {
                        outRect.bottom = spacing;
                    }
                } else {
                    if (isFirstColumn(index)) {
                        outRect.left = 0;
                        outRect.right = halfSpacing;
                    } else if (isLastColumn(index)) {
                        outRect.left = halfSpacing;
                        outRect.right = spacing;
                    } else {
                        outRect.left = halfSpacing;
                        if (isLastPosition(index)) {
                            outRect.right = spacing;
                        } else {
                            outRect.right = halfSpacing;
                        }
                    }
                }
                if (!isLastRow(index)) {
                    outRect.bottom = spacing;
                }
            }
        }
    }
}
